﻿@page "/identity/roles"
@attribute [Authorize]
@using Volo.Abp.Identity
@using Microsoft.AspNetCore.Authorization
@using Microsoft.Extensions.Localization
@using Volo.Abp.Identity.Localization
@using Volo.Abp.PermissionManagement.Blazor.Components
@inherits RoleManagementBase
@inject IStringLocalizer<IdentityResource> L
@* ************************* PAGE HEADER ************************* *@
<Row>
    <Column ColumnSize="ColumnSize.Is6">
        <h1>@L["Roles"]</h1>
    </Column>
    <Column ColumnSize="ColumnSize.Is6">
        <Paragraph Alignment="TextAlignment.Right">
            <Button Color="Color.Primary" Clicked="OpenCreateModalAsync">@L["NewRole"]</Button>
        </Paragraph>
    </Column>
</Row>

@* ************************* DATA GRID ************************* *@
<DataGrid TItem="IdentityRoleDto"
          Data="Entities"
          ReadData="OnDataGridReadAsync"
          TotalItems="TotalCount"
          ShowPager="true"
          PageSize="PageSize">
    <DataGridColumns>
        <DataGridColumn Width="150px" TItem="IdentityRoleDto" Field="@nameof(IdentityRoleDto.Name)" Sortable="false" Caption="@L["Actions"].Value">
            <DisplayTemplate>
                <Dropdown>
                    <DropdownToggle Color="Color.Primary">
                        @L["Actions"]
                    </DropdownToggle>
                    <DropdownMenu>
                        <DropdownItem Clicked="() => OpenEditModalAsync(context.As<IdentityRoleDto>().Id)">@L["Edit"]</DropdownItem>
                        <DropdownItem Clicked="() => PermissionManagementModal.OpenAsync(PermissionProviderName,context.As<IdentityRoleDto>().Name)">@L["Permissions"]</DropdownItem>
                        <DropdownDivider/>
                        <DropdownItem Clicked="() => DeleteEntityAsync(context.As<IdentityRoleDto>())">@L["Delete"]</DropdownItem>
                    </DropdownMenu>
                </Dropdown>
            </DisplayTemplate>
        </DataGridColumn>
        <DataGridColumn TItem="IdentityRoleDto" Field="@nameof(IdentityRoleDto.Name)" Caption="@L["RoleName"].Value">
            <DisplayTemplate>
                @(context.As<IdentityRoleDto>().Name)
                @if (context.As<IdentityRoleDto>().IsDefault)
                {
                    <Badge Color="Color.Primary">@L["DisplayName:IsDefault"]</Badge>
                }
                @if (context.As<IdentityRoleDto>().IsPublic)
                {
                    <Badge Color="Color.Light">@L["DisplayName:IsPublic"]</Badge>
                }
            </DisplayTemplate>
        </DataGridColumn>
    </DataGridColumns>
</DataGrid>

@* ************************* CREATE MODAL ************************* *@
<Modal @ref="CreateModal">
    <ModalBackdrop />
    <ModalContent IsCentered="true">
        <ModalHeader>
            <ModalTitle>@L["NewRole"]</ModalTitle>
            <CloseButton Clicked="CloseCreateModalAsync" />
        </ModalHeader>
        <ModalBody>
            <Field>
                <FieldLabel>@L["DisplayName:RoleName"]</FieldLabel>
                <TextEdit @bind-text="NewEntity.Name" />
            </Field>
            <Field>
                <Check TValue="bool" @bind-checked="@NewEntity.IsDefault">@L["DisplayName:IsDefault"]</Check>
                <Check TValue="bool" @bind-checked="@NewEntity.IsPublic">@L["DisplayName:IsPublic"]</Check>
            </Field>
        </ModalBody>
        <ModalFooter>
            <Button Color="Color.Secondary" Clicked="CloseCreateModalAsync">@L["Cancel"]</Button>
            <Button Color="Color.Primary" Clicked="CreateEntityAsync">@L["Save"]</Button>
        </ModalFooter>
    </ModalContent>
</Modal>

@* ************************* EDIT MODAL ************************* *@
<Modal @ref="EditModal">
    <ModalBackdrop />
    <ModalContent IsCentered="true">
        <ModalHeader>
            <ModalTitle>Edit role</ModalTitle>
            <CloseButton Clicked="CloseEditModalAsync" />
        </ModalHeader>
        <ModalBody>
            <input type="hidden" name="ConcurrencyStamp" @bind-value="EditingEntity.ConcurrencyStamp" />
            <Field>
                <FieldLabel>@L["DisplayName:RoleName"]</FieldLabel>
                <TextEdit @bind-text="EditingEntity.Name" />
            </Field>
            <Field>
                <Check TValue="bool" @bind-checked="@EditingEntity.IsDefault">@L["DisplayName:IsDefault"]</Check>
                <Check TValue="bool" @bind-checked="@EditingEntity.IsPublic">@L["DisplayName:IsPublic"]</Check>
            </Field>
        </ModalBody>
        <ModalFooter>
            <Button Color="Color.Secondary" Clicked="CloseEditModalAsync">@L["Cancel"]</Button>
            <Button Color="Color.Primary" Clicked="UpdateEntityAsync">@L["Save"]</Button>
        </ModalFooter>
    </ModalContent>
</Modal>

<PermissionManagementModal @ref="PermissionManagementModal"/>
